const responseMessage = require("../response/response_message");
const jwt = require("jsonwebtoken");
const config = require("../config");
const event = require("../utils/events");
const {
  registerApi,
  loginApi,
  getUserFriendsApi,
  addFriendApi,
  updateLineStatusApi,
  getUserIdByQq
} = require("../papi/user_api");

exports.getUserIdByQq = async (ctx) => {
  try {
    const { qq_account } = ctx.request.query;
    const user = await getUserIdByQq(qq_account);
    if(user){
        return (ctx.body = new responseMessage(201, "success", user));
    }else{
        return (ctx.body = new responseMessage(201, "用户不存在", {}));
    }
  } catch (error) {
    return (ctx.body = new responseMessage(500, error.message, {}));
  }
};

/**
 * 用户注册
 */
exports.register = async (ctx) => {
  try {
    const { qq_account, nick_name } = ctx.request.body;
    const isUserExists = await loginApi(qq_account);
    if (isUserExists) {
      return (ctx.body = new responseMessage(
        201,
        "QQ号已存在，请重新注册",
        {}
      ));
    }
    const user = await registerApi(qq_account, nick_name);
    return (ctx.body = new responseMessage(201, "success", user));
  } catch (error) {
    return (ctx.body = new responseMessage(500, error.message, {}));
  }
};

/**
 * 用户登录
 */
exports.login = async (ctx) => {
  try {
    const { qq_account } = ctx.request.body;
    const user = await loginApi(qq_account);
    if (!user) {
      return (ctx.body = new responseMessage(201, "该用户不存在", {}));
    }
    // set expiration time
    user.exp = Math.floor(Date.now() / 1000) + config.tokenExpired;
    const token = jwt.sign({ ...user }, config.screct);
    return (ctx.body = new responseMessage(201, "success", { user, token }));
  } catch (error) {
    return (ctx.body = new responseMessage(500, error.message, {}));
  }
};

/**
 * 获取用户好友列表
 */
exports.get_user_friends = async (ctx) => {
  try {
    const { _id } = ctx.request.query;
    const friends = await getUserFriendsApi(_id);
    // 发通知
    event.on("notifyUserLineStatus", (friends) => {
      console.log(friends);
    });
    return (ctx.body = new responseMessage(201, "success", friends));
  } catch (error) {
    return (ctx.body = new responseMessage(500, error.message, {}));
  }
};

/**
 * 添加好友
 */
exports.add_friend = async (ctx) => {
  try {
    const { myId, friendId } = ctx.request.body;
    const { modifiedCount } = await addFriendApi(myId, friendId);
    if (modifiedCount > 0) {
      return (ctx.body = new responseMessage(
        201,
        "success",
        await getUserFriendsApi(myId)
      ));
    } else {
      return (ctx.body = new responseMessage(201, "好友已存在", {}));
    }
  } catch (error) {
    return (ctx.body = new responseMessage(500, error.message, {}));
  }
};

/**
 * 修改用户在线状态
 */
exports.update_line_status = async (ctx) => {
  try {
    const { _id, qq_account, line_status } = ctx.request.body;
    const { modifiedCount } = await updateLineStatusApi(_id, line_status);
    if (modifiedCount > 0) {
      const u = await loginApi(qq_account);
      event.emit("notifyUserLineStatus", u?.good_friends);
      return (ctx.body = new responseMessage(201, "success", u));
    } else {
      return (ctx.body = new responseMessage(201, "切换失败", {}));
    } 
  } catch (error) {
    return (ctx.body = new responseMessage(500, error.message, {}));
  }
};   
 